package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.JsonResult.JsonResult;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.cache.service.util.RedisKeys;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.vo.PersonalVO;
import com.alibaba.fastjson.JSON;
import io.netty.util.internal.EmptyArrays;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Pageable;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
public class PersonalController {
    @Autowired
    private IPersonalService personalService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private StringRedisTemplate template;

    @GetMapping("/users/get")
    public Object get(String id) {
        UserInfo owner = userInfoService.get(id);
        return JsonResult.success(owner);
    }

    @GetMapping("/users/visitors")
    public Object visitors(String id, @UserParam UserInfo userInfo) {
        //判断访问用户是否是当前登录用户
        String userInfoId= userInfoService.getIdByPhone(userInfo.getPhone());
        String todayKeySTR = RedisKeys.TODAYVISITNUM.join(id);
        userInfo.setId(userInfoId);
        if (userInfo.getId().equals(id)) {
            String key = RedisKeys.PERSONAL_VO.join(id);

            String todayVisitNum = template.opsForValue().get(todayKeySTR);
            if (StringUtils.hasLength(todayKeySTR)){
                template.opsForValue().set(todayKeySTR,"0");
            }
            PersonalVO personalVO = new PersonalVO();
            if (template.hasKey(key)){
                personalVO = JSON.parseObject(template.opsForValue().get(key), PersonalVO.class);
            }
            if (personalService.getByUserId(id) != null) {
                Personal personal = personalService.getByUserId(id);
                BeanUtils.copyProperties(personal, personalVO);
            }
            template.opsForValue().set(key, JSON.toJSONString(personalVO));
            List<UserInfo> visitUsers =  new ArrayList<>();
            if (personalVO.getVisitUsers() != null){
                visitUsers = personalVO.getVisitUsers();
                Collections.reverse(visitUsers);
                visitUsers.subList(0,7);
            }
            personalVO.setVisitUsers(visitUsers);
            Map map=new HashMap();
            map.put("personalVO",personalVO);
            map.put("todayVisitNum",todayVisitNum);
            return JsonResult.success(map);
        }
        List<UserInfo> list=new ArrayList<>();
        String key = RedisKeys.PERSONAL_VO.join(id);
        PersonalVO personalVO = new PersonalVO();
        if (template.hasKey(key)) {
            personalVO = JSON.parseObject(template.opsForValue().get(key), PersonalVO.class);
        }else {
            if (personalService.getByUserId(id) != null) {
                Personal personal = personalService.getByUserId(id);
                BeanUtils.copyProperties(personal, personalVO);
            }
        }
        personalVO.setUserId(id);
        if ( !userInfo.getId().equals(id)) {
            if (!template.hasKey(todayKeySTR)){
                template.opsForValue().set(todayKeySTR,"1",RedisKeys.TODAYVISITNUM.getTime(), TimeUnit.SECONDS);
            }else {
                template.opsForValue().increment(todayKeySTR);
            }
            personalVO.setTotalVisitNum(personalVO.getTotalVisitNum() + 1);
            List<UserInfo> visitUsers = personalVO.getVisitUsers();
            visitUsers.add(userInfo);
            int a = 0;
            for (int i = visitUsers.size() - 1; i > 0; i--) {
                a++;
                UserInfo userInfo1 = visitUsers.get(i);
                list.add(userInfo1);
                personalVO.setVisitUsers(visitUsers);
                    if (a==8){
                        break;
                    }
                }
            }
        String todayVisitNum = template.opsForValue().get(todayKeySTR);
        template.opsForValue().set(key, JSON.toJSONString(personalVO));
        personalVO.setVisitUsers(list);
        Map map=new HashMap();
        map.put("personalVO",personalVO);
        map.put("todayVisitNum",todayVisitNum);
        return JsonResult.success(map);
    }

    @GetMapping("/users/list")
    public Object list(String id, @UserParam UserInfo userInfo) {
        String key = RedisKeys.PERSONAL_VO.join(id);
        String personalVOstr = template.opsForValue().get(key);
        PersonalVO personalVO = new PersonalVO();
        if (personalVOstr != null) {
            personalVO = JSON.parseObject(personalVOstr, PersonalVO.class);
        }
        if (personalService.getByUserId(id) != null) {
            Personal personal = personalService.getByUserId(id);
            BeanUtils.copyProperties(personal, personalVO);
        }
        template.opsForValue().set(key, JSON.toJSONString(personalVO));
        List<UserInfo> visitUsers = personalVO.getVisitUsers();
        Collections.reverse(visitUsers);
        personalVO.setVisitUsers(visitUsers);
        return JsonResult.success(personalVO);
    }
}
